package com.lzs.chuku.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzs.chuku.mapper.UserMapper;
import com.lzs.entity.Picktask;
import com.lzs.chuku.service.OutorderService;
import com.lzs.chuku.service.PicktaskService;
import com.lzs.entity.User;
import com.lzs.util.JwtUtils;
import com.lzs.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘中帅
 * @since 2020-11-23
 */
@RestController
@RequestMapping("/chuku/picktask")
public class PicktaskController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PicktaskService picktaskService;
    @Autowired
    private OutorderService outorderService;
    @PostMapping("addPickTask")
    @ApiOperation("生成拣货任务")
    public R addPickTask(@RequestBody List<Long> outorderids, HttpServletRequest request){
        if (outorderids.size()==0)
            return R.error().message("请选择任一选项");
        String token = request.getHeader("token");
        Long userId= JwtUtils.getMemberIdByJwtToken(token);
        User user = userMapper.findUserbyId(userId);
        return picktaskService.addPickTask(outorderids,user);
    }

    @PostMapping("findByPicktaskPage/{currentPage}/{pageSize}")
    @ApiOperation("查询所有拣货任务")
    public R findAllPIcktask(@RequestBody Picktask picktask,
                             @PathVariable("currentPage") Integer currentPage,
                             @PathVariable("pageSize") Integer pageSize){
       return picktaskService.findByPicktaskPage(picktask,currentPage,pageSize);
    }

    @GetMapping("rollBackPicktask/{id}")
    @ApiOperation("还原拣货任务")
    public R rollBackPicktask(@PathVariable("id") Long picktaskid){
        return picktaskService.rollBackPicktask(picktaskid);
    }
    @GetMapping("findAllpicktask")
    @ApiOperation("查询所有拣货任务（下拉使用）")
    public R findAllpicktask(){
        QueryWrapper<Picktask> wrapper=new QueryWrapper<>();
        wrapper.eq("state","0");
        List<Picktask> list = picktaskService.list(wrapper);
        return R.ok().message("查询成功").data("picktaskList",list);
    }
}

